package net.soporte.action;

import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.soporte.dao.EmpresaDAO;
import net.soporte.dao.SucursalDAO;
import net.soporte.form.SucursalForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class SucursalLstAction extends DispatchAction {
	
		public ActionForward agregar(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			String target = null;
			List empresaList;
			
			SucursalForm sucursalForm = (SucursalForm) form;
			sucursalForm.setDireccion("");
			sucursalForm.setTelefono("");
			sucursalForm.setNombreSucursal("");
			
			EmpresaDAO empresaDAO = new EmpresaDAO();
			empresaList=empresaDAO.ListarDatos(null);
			
			sucursalForm.setEmpresaList(empresaList);
			
			target = "success";
			request.setAttribute("accion", "Agregar");
			
			return mapping.findForward(target);
		}
		
		public ActionForward editar(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			String target = null;
			SucursalForm sucursalForm = (SucursalForm) form;
			SucursalDAO sucursalDAO = new SucursalDAO();
			
			sucursalForm.setCodigoEmpresa(Integer.parseInt(request.getParameter("codigoEmpresa")));
			sucursalForm.setCodigoSucursal(Integer.parseInt(request.getParameter("codigoSucursal")));
			
			sucursalDAO.Buscar(sucursalForm);
						
			request.setAttribute("accion", "Actualizar");
			target = "success";
			return mapping.findForward(target);
		}
		
		public ActionForward eliminar(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			String target = null;
			SucursalForm sucursalForm = (SucursalForm) form;
			SucursalDAO sucursalDAO = new SucursalDAO();
			
			sucursalForm.setCodigoEmpresa(Integer.parseInt(request.getParameter("codigoEmpresa")));
			sucursalForm.setCodigoSucursal(Integer.parseInt(request.getParameter("codigoSucursal")));
			
			if (sucursalDAO.Borrar(sucursalForm)==0){
				List listSucursales=new Vector();	
				//utilizar DAO			
				listSucursales=sucursalDAO.ListarDatos(form);
				request.setAttribute("ls", listSucursales);
				target = "delete";	
			}else{
				target = "failure";	
			}
			
			return mapping.findForward(target);
		}
		

}
